// pages/group/group.js
Page({
  changeCategory: function (e) {
    this.setData({
      category: e.currentTarget.id
    });
  },

groupname:function(e){
  this.setData({
    groupname:e.detail.value
  })
},
groupnum:function(e){
  this.setData({
    groupnum:e.detail.value
  })
},
tijiao(){
  var that = this;
  var database = wx.cloud.database();
  database.collection('tb_group').add({
    data:{
      groupname:that.data.groupname,
      groupnum:that.data.groupnum
    }
  }).then((res)=>{
    if(res){
      wx.showToast({
        title:'创建小组成功',
      })
    }
  })
},

shuru:function(e){
  this.setData({shuru:e.detail.value});
},
sousuo(){
  var that = this;
  var database = wx.cloud.database();
  database.collection("tb_group").where({}).get().then((res)=>{
    if(res){
      var result=[];
      for(let i=0;i<res.data.length;i++){
        if(res.data[i].groupname.match(that.data.shuru)){
          result.push(res.data[i]);
        }
      }    
      that.setData({result:result})
    }
  })
},

dianji:function(e){
  var id = e.currentTarget.id;
  var group = this.data.result[id];
  var that = this;
  var data = {
    groupid:group._id,
    groupname:group.groupname,
    userid:that.data.userid,
    username:that.data.username
  }
var database = wx.cloud.database();
database.collection("tb_rudui").where({userid:that.data.userid}).get().then((res)=>{
  if(res&&res.data.length>0){
    wx.showToast({
      title: '不能重复入队',
    })
  }else{
    var num = group.groupnum;
    database.collection("tb_rudui").where({groupid:group._id}).get().then((res)=>{
      if(res&&res.data.length==num){
        wx.showToast({
          title: '该小队已满人',
        })
      }else{ database.collection("tb_rudui").add({data:data}).then((res)=>{
        if(res){
          wx.showToast({
            title: '入队成功',
          })
        }
      })
    }
    })

      }
    })
},
    /**
     * 页面的初始数据
     */
    data: {
      titles:[{'id':1,'name':'建队'},{'id':2,'name':'入队'}],
      category:0,
      index:0,
      groupname:null,
      groupnum:null,
      shuru:null,
      resulet:[],
      userid:null,
      username:null
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
      var that = this;
      wx.getStorage({
        key:'userid',
        success(res){
          if(res){
            that.setData({userid:res.data});
            var database =wx.cloud.database();
            database.collection("tb_user").where({_id:that.data.userid}).get().then((res)=>{
              if(res){
                that.setData({username:res.data[0].name})
              }
            })
          }
        }
      })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})